package cn.edu.ujn.food.service;

import cn.edu.ujn.food.entity.Category;
import cn.edu.ujn.food.dao.CategoryDao;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;
    @Override
    public cn.edu.ujn.util.Page<Category> selectAll(Category category) {
        List<Category> categories = categoryDao.selectAll(category);
        cn.edu.ujn.util.Page<Category> page = new cn.edu.ujn.util.Page<>();
        page.setPage(category.getStart());
        page.setRows(categories);
        page.setSize(category.getRows());
        page.setTotal(categoryDao.selectCount());
        return page;
    }

    @Override
    public List<Category> findAll() {
        return categoryDao.findAll();
    }

    @Override
    public Category queryById(Integer cateId) {
        return this.categoryDao.queryById(cateId);
    }

    @Override
    public Page<Category> queryByPage(Category category, PageRequest pageRequest) {
        long total = this.categoryDao.count(category);
        return new PageImpl<>(this.categoryDao.queryAllByLimit(category, pageRequest), pageRequest, total);
    }

    @Override
    public Category insert(Category category) {
        this.categoryDao.insert(category);
        return category;
    }

    @Override
    public Category update(Category category) {
        this.categoryDao.update(category);
        return this.queryById(category.getCateId());
    }

    @Override
    public boolean deleteById(Integer cateId) {
        return this.categoryDao.deleteById(cateId) > 0;
    }


}
